package common.code;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class AppUtils {
	/**
	 * 加载properties 文件属性
	 * @param fileName
	 * @return
	 */
	  public static Properties loadProperties(String fileName)
	  {
		Properties props=null;
		try {
			  props = new Properties();
			  InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
			  props.load(inputStream);
		} catch (IOException e) {
			  e.printStackTrace();
			  new RuntimeException();
		} 
		  return props;
	  }
	  
	  /**
	   * 获取ApplicationConfig.properties文件中的配置数据
	   * @param parameData
	   * @return
	   */
	  public static String getDataFromProperties(String parameData)
	  {
		Properties props=loadProperties("/ApplicationConfig.properties");
		return props.getProperty(parameData);
	  }
	  
	  /**
	   * 生成resData数据
	   * @param headMap
	   * @param bodyList
	   * @return
	   */
		public static String generateResData(Map headMap,List bodyList)
		{
			JSONObject jsonHead=JSONObject.fromObject(headMap);
			JSONArray jsonBody=JSONArray.fromObject(bodyList);
			String headStr="{\"head\":"+jsonHead.toString()+",";
			String bodyStr="\"body\":{\"result\":"+jsonBody.toString()+"}}";
			return headStr+bodyStr;
		}
		
		
		public static String generateResData(Map headMap,Map bodyMap)
		{
			JSONObject jsonHead=JSONObject.fromObject(headMap);
			JSONArray jsonBody=JSONArray.fromObject(bodyMap);
			String headStr="{\"head\":"+jsonHead.toString()+",";
			String bodyStr="\"body\":{\"result\":"+jsonBody.toString()+"}}";
			return headStr+bodyStr;
		}
		
		
}
